package json;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.Since;


/**
 * @Author: ycy
 * @CreateDate: 2019/5/30 18:02
 */
public class SinaAndUnitl {

  public static void main(String[] args) {
    //对象 转 String , 低于gson的版本都能用
    Gson gson = new GsonBuilder().setVersion(1.2).create();
    PackageMaterial packageMaterial = new PackageMaterial();
    packageMaterial.setName("1.2版本的哈哈");
    packageMaterial.setItag("1.1版本的哈哈");
    String x = gson.toJson(packageMaterial);
    System.out.println(x);

    String newX = "{\"name\":\"1.2版本的哈哈\",\"itag\":\"1.1版本的哈哈\"}";
    //String 转 对象 ,
    Gson gson1 = new GsonBuilder().setVersion(1.1).create();
    PackageMaterial packageMaterial1 = gson1.fromJson(newX, PackageMaterial.class);
    System.out.println(packageMaterial1);
  }


  private static class PackageMaterial {

    //1.2 才开始启用
    @Since(1.2)
    private String name;

    //1.1 才开始启用
    @Since(1.1)
    private String itag;
    private String cover_square;
    private String cover_horizon;
    private String cover_vertical;

    public PackageMaterial() {
    }

    public String getName() {
      return name;
    }

    public void setName(String name) {
      this.name = name;
    }

    public String getItag() {
      return itag;
    }

    public void setItag(String itag) {
      this.itag = itag;
    }

    public String getCover_square() {
      return cover_square;
    }

    public void setCover_square(String cover_square) {
      this.cover_square = cover_square;
    }

    public String getCover_horizon() {
      return cover_horizon;
    }

    public void setCover_horizon(String cover_horizon) {
      this.cover_horizon = cover_horizon;
    }

    public String getCover_vertical() {
      return cover_vertical;
    }

    public void setCover_vertical(String cover_vertical) {
      this.cover_vertical = cover_vertical;
    }

    @Override
    public String toString() {
      return "PackageMaterial{" +
              "name='" + name + '\'' +
              ", itag='" + itag + '\'' +
              ", cover_square='" + cover_square + '\'' +
              ", cover_horizon='" + cover_horizon + '\'' +
              ", cover_vertical='" + cover_vertical + '\'' +
              '}';
    }
  }

}
